﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using TIMETABLEASY.iCalManager;

using TIMETABLEASY.EasyCalendar.Model;
using TIMETABLEASY.EasyCalendar.Controler;

using TIMETABLEASY.TokenAuthentification;

namespace TIMETABLEASY
{
    public partial class Window1
    {
        private void display_items_expander_offline()
        {
            type = planning_expend_comboBox.SelectedValue.ToString();
            switch (type)
            {

                //Droits : admin
                case "University":
                    {
                        campus_expend_planning_combobox.Visibility = Visibility.Hidden;
                        campus_label_expand.Visibility = Visibility.Hidden;
                        cursus_expend_planning_combobox.Visibility = Visibility.Hidden;
                        cursus_label_expand.Visibility = Visibility.Hidden;
                        class_expend_planning_combobox.Visibility = Visibility.Hidden;
                        class_label_expand.Visibility = Visibility.Hidden;
                        period_expend_planning_combobox.Visibility = Visibility.Hidden;
                        period_label_expand.Visibility = Visibility.Hidden;
                        user_expend_planning_combobox.Visibility = Visibility.Hidden;
                        user_label_expand.Visibility = Visibility.Hidden;
                        type_user_expand_combobox.Visibility = Visibility.Hidden;
                        type_user_label_expand.Visibility = Visibility.Hidden;

                        break;
                    }

                //Droits : Admin/Manager/Teacher/Campus
                case "Campus":
                    {
                        campus_expend_planning_combobox.Items.Clear();
                        campus_expend_planning_combobox.Visibility = Visibility.Visible;
                        campus_label_expand.Visibility = Visibility.Visible;
                        cursus_expend_planning_combobox.Visibility = Visibility.Hidden;
                        cursus_label_expand.Visibility = Visibility.Hidden;
                        class_expend_planning_combobox.Visibility = Visibility.Hidden;
                        class_label_expand.Visibility = Visibility.Hidden;
                        period_expend_planning_combobox.Visibility = Visibility.Hidden;
                        period_label_expand.Visibility = Visibility.Hidden;
                        user_expend_planning_combobox.Visibility = Visibility.Hidden;
                        user_label_expand.Visibility = Visibility.Hidden;
                        type_user_expand_combobox.Visibility = Visibility.Hidden;
                        type_user_label_expand.Visibility = Visibility.Hidden;

                        if (usertype == "administrators")
                        {

                            foreach (Campus s in (from e in db_local.Campus select e))
                            {
                                campus_expend_planning_combobox.Items.Add(s.Name);
                            }
                        }

                        else if (usertype == "managers")
                        {

                            //Récupération, découpage et affichage des campus enregistrés pour un manager
                            string[] table_campus;
                            string registred_campus = usercampus;
                            table_campus = registred_campus.Split('|');

                            for (int i = 0; i < table_campus.Length; i++)
                            {
                                campus_expend_planning_combobox.Items.Add(table_campus[i]);
                            }


                        }

                        else if (usertype == "teachers")
                        {
                            campus_expend_planning_combobox.Items.Add(usercampus);
                        }

                        else if (usertype == "students")
                        {
                            campus_expend_planning_combobox.Items.Add(usercampus);
                        }


                        break;
                    }

                //Droits : admin/manager/teacher/student
                case "Class":
                    {
                        campus_expend_planning_combobox.Items.Clear();
                        campus_expend_planning_combobox.Visibility = Visibility.Visible;
                        campus_label_expand.Visibility = Visibility.Visible;
                        cursus_expend_planning_combobox.Visibility = Visibility.Hidden;
                        cursus_label_expand.Visibility = Visibility.Hidden;
                        class_expend_planning_combobox.Visibility = Visibility.Visible;
                        class_label_expand.Visibility = Visibility.Visible;
                        period_expend_planning_combobox.Visibility = Visibility.Hidden;
                        period_label_expand.Visibility = Visibility.Hidden;
                        user_expend_planning_combobox.Visibility = Visibility.Hidden;
                        user_label_expand.Visibility = Visibility.Hidden;
                        type_user_expand_combobox.Visibility = Visibility.Hidden;
                        type_user_label_expand.Visibility = Visibility.Hidden;

                        if (usertype == "administrators")
                        {

                            foreach (Campus s in (from e in db_local.Campus select e))
                            {
                                campus_expend_planning_combobox.Items.Add(s.Name);
                            }
                        }

                        else if (usertype == "managers")
                        {

                            //Récupération, découpage et affichage des campus enregistrés pour un manager
                            string[] table_campus;
                            string registred_campus = usercampus;
                            table_campus = registred_campus.Split('|');

                            for (int i = 0; i < table_campus.Length; i++)
                            {
                                campus_expend_planning_combobox.Items.Add(table_campus[i]);
                            }


                        }

                        else if (usertype == "teachers")
                        {
                            campus_expend_planning_combobox.Items.Add(usercampus);
                        }

                        else if (usertype == "students")
                        {
                            campus_expend_planning_combobox.Items.Add(usercampus);
                        }

                        break;
                    }

                //Droits : admin/manager
                case "Cursus":
                    {
                        campus_expend_planning_combobox.Visibility = Visibility.Hidden;
                        campus_label_expand.Visibility = Visibility.Hidden;
                        cursus_expend_planning_combobox.Visibility = Visibility.Visible;
                        cursus_label_expand.Visibility = Visibility.Visible;
                        class_expend_planning_combobox.Visibility = Visibility.Hidden;
                        class_label_expand.Visibility = Visibility.Hidden;
                        period_expend_planning_combobox.Visibility = Visibility.Hidden;
                        period_label_expand.Visibility = Visibility.Hidden;
                        user_expend_planning_combobox.Visibility = Visibility.Hidden;
                        user_label_expand.Visibility = Visibility.Hidden;
                        type_user_expand_combobox.Visibility = Visibility.Hidden;
                        type_user_label_expand.Visibility = Visibility.Hidden;


                        foreach (Cursus s in (from e in db_local.Cursus select e))
                        {
                            cursus_expend_planning_combobox.Items.Add(s.Name);
                        }

                        break;
                    }


                //Droits : admin/manager
                case "Period":
                    {
                        campus_expend_planning_combobox.Items.Clear();
                        campus_expend_planning_combobox.Visibility = Visibility.Visible;
                        campus_label_expand.Visibility = Visibility.Visible;
                        cursus_expend_planning_combobox.Visibility = Visibility.Hidden;
                        cursus_label_expand.Visibility = Visibility.Hidden;
                        class_expend_planning_combobox.Visibility = Visibility.Hidden;
                        class_label_expand.Visibility = Visibility.Hidden;
                        period_expend_planning_combobox.Visibility = Visibility.Visible;
                        period_label_expand.Visibility = Visibility.Visible;
                        user_expend_planning_combobox.Visibility = Visibility.Hidden;
                        user_label_expand.Visibility = Visibility.Hidden;
                        type_user_expand_combobox.Visibility = Visibility.Hidden;
                        type_user_label_expand.Visibility = Visibility.Hidden;

                        if (usertype == "administrators")
                        {

                            foreach (Campus s in (from e in db_local.Campus select e))
                            {
                                campus_expend_planning_combobox.Items.Add(s.Name);
                            }
                        }

                        else if (usertype == "managers")
                        {

                            //Récupération, découpage et affichage des campus enregistrés pour un manager
                            string[] table_campus;
                            string registred_campus = usercampus;
                            table_campus = registred_campus.Split('|');

                            for (int i = 0; i < table_campus.Length; i++)
                            {
                                campus_expend_planning_combobox.Items.Add(table_campus[i]);
                            }


                        }

                        else if (usertype == "teachers")
                        {
                            campus_expend_planning_combobox.Items.Add(usercampus);
                        }

                        else if (usertype == "students")
                        {
                            campus_expend_planning_combobox.Items.Add(usercampus);
                        }


                        break;
                    }


                case "User":
                    {
                        campus_expend_planning_combobox.Items.Clear();
                        campus_expend_planning_combobox.Visibility = Visibility.Visible;
                        campus_label_expand.Visibility = Visibility.Visible;
                        cursus_expend_planning_combobox.Visibility = Visibility.Hidden;
                        cursus_label_expand.Visibility = Visibility.Hidden;
                        class_expend_planning_combobox.Visibility = Visibility.Hidden;
                        class_label_expand.Visibility = Visibility.Hidden;
                        period_expend_planning_combobox.Visibility = Visibility.Hidden;
                        period_label_expand.Visibility = Visibility.Hidden;
                        user_expend_planning_combobox.Visibility = Visibility.Visible;
                        user_label_expand.Visibility = Visibility.Visible;
                        type_user_expand_combobox.Visibility = Visibility.Visible;
                        type_user_label_expand.Visibility = Visibility.Visible;


                        if (usertype == "administrators")
                        {

                            foreach (Campus s in (from e in db_local.Campus select e))
                            {
                                campus_expend_planning_combobox.Items.Add(s.Name);
                            }
                        }

                        else if (usertype == "managers")
                        {

                            //Récupération, découpage et affichage des campus enregistrés pour un manager
                            string[] table_campus;
                            string registred_campus = usercampus;
                            table_campus = registred_campus.Split('|');

                            for (int i = 0; i < table_campus.Length; i++)
                            {
                                campus_expend_planning_combobox.Items.Add(table_campus[i]);
                            }


                        }

                        else if (usertype == "teachers")
                        {
                            campus_expend_planning_combobox.Items.Add(usercampus);
                        }

                        else if (usertype == "students")
                        {
                            campus_expend_planning_combobox.Items.Add(usercampus);
                        }

                        break;
                    }
            }
        }

        private void display_user_type_expander_offline()
        {
            string selected_usertype = type_user_expand_combobox.SelectedValue.ToString();
            switch (selected_usertype)
            {
                case "administrator":
                    user_expend_planning_combobox.Items.Clear();
                    foreach (Administrators s2 in (from a in db_local.Administrators select a))
                    {
                        user_expend_planning_combobox.Items.Add(s2.Surname);
                    }
                    break;
                case "manager":
                    user_expend_planning_combobox.Items.Clear();
                    foreach (Managers s2 in (from a in db_local.Managers select a))
                    {
                        user_expend_planning_combobox.Items.Add(s2.Surname);
                    }
                    break;
                case "teacher":
                    user_expend_planning_combobox.Items.Clear();
                    foreach (Teacher s2 in (from a in db_local.Teacher select a))
                    {
                        user_expend_planning_combobox.Items.Add(s2.Surname);
                    }
                    break;
                case "student":
                    user_expend_planning_combobox.Items.Clear();
                    foreach (Students s2 in (from a in db_local.Students select a))
                    {
                        user_expend_planning_combobox.Items.Add(s2.Surname);
                    }
                    break;
                default:

                    break;
            }
        }

        private void submit_new_planning_offline()
        {
            SingletonTokenUser glegle = SingletonTokenUser.GetInstance();
            glegle.TypeOfView = type;

            switch (type)
            {
                case "University":

                    break;
                case "Campus":

                    if (campus_expend_planning_combobox.SelectedValue.ToString() != "")
                    {
                        string selected_campus = campus_expend_planning_combobox.SelectedValue.ToString();

                        int selected_campus_id = (from a in db_local.Campus
                                                  where a.Name == selected_campus
                                                  select a.Id).FirstOrDefault();

                        glegle.ValueOfView = selected_campus_id;
                    }


                    break;

                case "Class":

                    if (campus_expend_planning_combobox.SelectedValue.ToString() != "")
                    {
                        string selected_campus = campus_expend_planning_combobox.SelectedValue.ToString();

                        int selected_campus_id = (from a in db_local.Campus
                                                  where a.Name == selected_campus
                                                  select a.Id).FirstOrDefault();


                        if (class_expend_planning_combobox.SelectedValue.ToString() != "")
                        {

                            string selected_class = class_expend_planning_combobox.SelectedValue.ToString();

                            int selected_class_id = (from a in db_local.Classes
                                                     where a.Name == selected_class && a.Campus_id == selected_campus_id
                                                     select a.Id).FirstOrDefault();

                            glegle.ValueOfView = selected_class_id;

                        }
                    }

                    break;

                case "Cursus":

                    if (cursus_expend_planning_combobox.SelectedValue.ToString() != "")
                    {
                        string selected_cursus = cursus_expend_planning_combobox.SelectedValue.ToString();

                        int selected_cursus_id = (from a in db_local.Cursus
                                                  where a.Name == selected_cursus
                                                  select a.Id).FirstOrDefault();

                        glegle.ValueOfView = selected_cursus_id;
                    }

                    break;

                case "Period":

                    if (cursus_expend_planning_combobox.SelectedValue.ToString() != "")
                    {
                        string selected_cursus = cursus_expend_planning_combobox.SelectedValue.ToString();

                        int selected_cursus_id = (from a in db_local.Cursus
                                                  where a.Name == selected_cursus
                                                  select a.Id).FirstOrDefault();

                        glegle.ValueOfView = selected_cursus_id;


                        if (period_expend_planning_combobox.SelectedValue.ToString() != "")
                        {
                            string selected_period = period_expend_planning_combobox.SelectedValue.ToString();

                            int selected_period_id = (from a in db_local.Period
                                                      where a.Name_period == selected_cursus_id + "?" + selected_period
                                                      select a.Id).FirstOrDefault();

                            glegle.ValueOfView = selected_period_id;
                        }
                    }

                    break;

                case "User":

                    if (user_expend_planning_combobox.SelectedValue.ToString() != "" && type_user_expand_combobox.SelectedValue.ToString() != "")
                    {

                        string selected_usertype = type_user_expand_combobox.SelectedValue.ToString();
                        string selected_user = user_expend_planning_combobox.SelectedValue.ToString();


                        if (selected_usertype == "administrator")
                        {

                            int selected_user_id = (from a in db_local.Administrators
                                                    where a.Login == selected_user
                                                    select a.Id).FirstOrDefault();

                            glegle.ValueOfView = selected_user_id;
                        }

                        else if (selected_usertype == "manager")
                        {

                            int selected_user_id = (from a in db_local.Managers
                                                    where a.Login == selected_user
                                                    select a.Id).FirstOrDefault();

                            glegle.ValueOfView = selected_user_id;
                        }

                        else if (selected_usertype == "teacher")
                        {

                            int selected_user_id = (from a in db_local.Teacher
                                                    where a.Login == selected_user
                                                    select a.Id).FirstOrDefault();

                            glegle.ValueOfView = selected_user_id;
                        }

                        else if (selected_usertype == "student")
                        {

                            int selected_user_id = (from a in db_local.Students
                                                    where a.Login == selected_user
                                                    select a.Id).FirstOrDefault();

                            glegle.ValueOfView = selected_user_id;
                        }


                    }

                    break;

                default:
                    break;
            }
        }

        private void display_campus_expander_offline()
        {
            int id = 0;
            string campus = "default";
            try
            {
                campus = campus_expend_planning_combobox.SelectedValue.ToString();
            }
            catch { }

            class_expend_planning_combobox.Items.Clear();
            period_expend_planning_combobox.Items.Clear();


            //Actions dans le cas d'un filtre campus
            if(planning_expend_comboBox.Text == "Campus")
            {
                id = (from a in db_local.Campus where a.Name == campus select a.Id).FirstOrDefault();
            }
            
            //Actions dans le cas d'un filtre classe
            else if (planning_expend_comboBox.Text == "Class")
            {
                id = (from a in db_local.Campus where a.Name == campus select a.Id).FirstOrDefault();

                foreach (Classes s2 in (from a in db_local.Classes where a.Campus_id == id select a))
                {
                    class_expend_planning_combobox.Items.Add(s2.Name);
                }
            }

            //Actions dans le cas d'un filtre période
            else if (planning_expend_comboBox.Text == "Period")
            {

                int current_campus_id = 0;

                current_campus_id = (from e in db_local.Campus
                                     where e.Name == campus
                                     select e.Id).FirstOrDefault();


                period_expend_planning_combobox.Items.Clear();

                foreach (Period_campus period_campus1 in (from e in db_local.Period_campus where e.Campus_id == current_campus_id select e))
                {
                    try
                    {
                        Period period1 = (from e in db_local.Period where e.Id == period_campus1.Period_id select e).FirstOrDefault();

                        string cursus_name = (from e in db_local.Cursus
                                             where e.Id == period1.Cursus_id
                                             select e.Name).FirstOrDefault().ToString();

                        string period_name_all = period1.Name_period;

                        string[] period_name = period_name_all.Split('?');
                        period_expend_planning_combobox.Items.Add(period_name[1] + " - cursus " + cursus_name);
                    }

                    catch (Exception)
                    {

                    }

                }
            }


        }
    }
}
